package com.gsm.phimhanhdong;

import java.util.ArrayList;
import java.util.Comparator;

import android.util.Log;

public class TimeComparator implements Comparator<Video> {
	int type ;
	public TimeComparator(int type){
		this.type= type;
	}
	
	public ArrayList<Integer> getTime(String duration) {
		int h = 0;
		int m = 0;
		int s = 0;
		Log.e("", "duration "+ duration);
		ArrayList<Integer>  rs = new ArrayList<Integer>();
		duration = duration.replace("PT", "");
		if (duration.contains("H")) {
			h = Integer
					.valueOf(duration.substring(0, duration.lastIndexOf("H")));
			duration = duration.substring(duration.indexOf("H")+1 , duration.length());
		}
		if (duration.contains("M")) {
			m = Integer
					.valueOf(duration.substring(0, duration.lastIndexOf("M")));
			duration = duration.substring(duration.indexOf("M")+1 , duration.length());
		}
		
		if (duration.contains("S")) {
			s = Integer
					.valueOf(duration.substring(0, duration.lastIndexOf("S")));
		}
		rs.add(h);
		rs.add(m);
		rs.add(s);
		return rs;
		
	}

	@Override
	public int compare(Video arg0, Video arg1) {
		// TODO Auto-generated method stub
		ArrayList<Integer> rs1 = getTime(arg0.videoinfo.duration);
		int time1 = rs1.get(0)*3600+rs1.get(1)*60+rs1.get(2);
		ArrayList<Integer> rs2 = getTime(arg1.videoinfo.duration);
		int time2 = rs2.get(0)*3600+rs2.get(1)*60+rs2.get(2);
		Log.e("", " time1 "+ time1+" time2 "+time2+" type "+type);
		int a =0;
		if(type ==0){
			if (time1 > time2)
				a= 1;
			else if (time1 < time2)
				a= -1;
			else
				a= 0;
		}else{
			if (time1 > time2)
				a= -1;
			else if (time1 < time2)
				a= 1;
			else
				a= 0;
		}
		Log.e("", "a "+a);
		return a;
		
	}

}
